package com.forte.forTest;

import java.io.File;
import java.util.*;

import org.junit.Test;

import com.forte.fortePoiUtils.PoiHelper;
import com.forte.fortePoiUtils.reader.ExcelBean;
import com.forte.fortePoiUtils.reader.ExcelReader;
import com.forte.fortePoiUtils.writer.ExcelWriter;

/**
 * 测试用
 */
public class Demo {

    /**
     * 写入Demo
     * @throws Exception
     */
	@Test
	public void demo1() throws Exception {

		//准备数据
		User user1 = new User("张三1" , "男" , 20);
		User user2 = new User("张三2" , "女" , 20);
		User user3 = new User("张三3" , "男" , 30);
		User user4 = new User("张三4" , "女" , 20);
		User user5 = new User("张三5" , "男" , 10);

		Child child = new Child();

		child.setName("小朋友~");

		user1.setChild(child);
		user2.setChild(child);
		user3.setChild(child);
		user4.setChild(child);
		user5.setChild(child);


		List<User> list = new ArrayList<>();
		Random r = new Random();
        for(int i = 0;i<1000;i++){
            list.add(new User("张三" + (i + 1) , r.nextBoolean()?"男":"女" , r.nextInt(40)+1));
        }


		List<User> list2 = new ArrayList<>();

        list2.add(user1);
        list2.add(user2);
        list2.add(user3);
        list2.add(user4);
        list2.add(user5);

		ExcelWriter writer = PoiHelper.getWriter(new File("F:/ExcelDemo/ss2.xlsx"));
		
		Map<String , String> result = new HashMap<>();
		result.put("username", "姓名");
		result.put("sex", "性别");
		result.put("age", "年龄");
		result.put("age", "年龄");
		result.put("child.name", "child年龄");


		//将数据写入文件 , 参数所代表的意义分别为:
        /*
         new User[]{user1, user2, user3, user4, user5} : 写入的数据的数值,用list大概也可以
         result :   字段的映射
         (a,b) -> a.startsWith("a")?-1:1 :  排序接口,可选
         3 :    起始行
         3 :    起始列

         */
//		boolean ok = writer.writeArrayToFile(new User[]{user1, user2, user3, user4, user5}, result , (a,b) -> a.startsWith("a")?-1:1 , 3 , 3);
		boolean ok = writer.writeList(list2, result , 3 , 3);

		if(ok)
			System.out.println("创建成功");
		else
			System.out.println("创建失败");
		
	}

    /**
     * 读取Demo
     * @throws Exception
     */
	@Test
	public void demo2() throws Exception{
		Map<String, String> maps = new HashMap<>();
		maps.put("年龄", "age");
		maps.put("姓名", "username");
		maps.put("性别", "sex");

		//要读取的文件
		File file = new File("F:\\ExcelDemo\\ss.xlsx");

		//获取Excel读取助手
		ExcelReader reader = PoiHelper.getReader(file);

		//读取并获取读取后的封装类
		ExcelBean<User> read = reader.read(User.class, maps, 0);

		//从封装类中获取数据List集合
        //封装类中除了数据集合以外还能获取到:
        // 1、查数据时所用的表的索引 - int
        // 2、查数据时所用的表的表名 - String
		List<User> list = read.getList();

        System.out.println(list.size());

		//打印展示数据
		list.forEach(System.out::println);
	}



	@Test
	public void demo3(){

		Map<String , Integer> map = new HashMap<>();

		for(int i = 1;i<= 26;i++){
			char c = (char)(97 + (i-1));
			map.put(c + "" , i);
		}


		map.forEach((k,v) -> System.out.println(k + " = " + v));

	}

}
